<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
	<head>
		<jsp:include page="../inc.jsp"></jsp:include>
		<script type="text/javascript" src="../My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript" src="../jslib/date.js"></script>
		<script type="text/javascript" charset="utf-8">
		var lastIndex;
		var tempid;
		var tempnum = 1;
		var tempbarcode = new Array();
		$(function() {
			$('#datebox').val((new Date()).pattern("yyyy-MM-dd"));
			$('#uid').val('${sessionInfo.userId}');
			
			var irid = $('#transferForm').find('input[name=irid]');
			var iridCombobox = irid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_Incombobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});
			
			var orid = $('#transferForm').find('input[name=orid]');
			var oridCombobox = orid.combobox({
				url : '${pageContext.request.contextPath}/rkgl/repertoryAction!noAuth_combobox.html',
				valueField : 'id',
				textField : 'name',
				multiple : false,
				editable : false,
				panelHeight : 'auto',
				onLoadSuccess : function() {
					if('${sessionInfo.repertoryIds}'!=null&&'${sessionInfo.repertoryIds}'!='null'){
						orid.combobox('setValue','${sessionInfo.repertoryIds}');
					}
					parent.$.messager.progress('close');
				},
			    onSelect : function(record){
			    $('#dg').datagrid('loadData', { total: 0, rows: [] });
					sumNum();
				    if(record.id!=null){
				    	$.ajax({
							url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
							data : {type : 'DB',rid : record.id},
							type : "POST",
							success : function(d){
								$('#serial').val(d);
							}
						});
				    }
				 }
			});
			
			var uid = $('#transferForm').find('input[name=uid]');
			var uidCombobox = uid.combobox({
				url : '${pageContext.request.contextPath}/qxgl/userAction!noAuth_userInReperCombobox.html',
				valueField : 'id',
				textField : 'username',
				multiple : false,
				editable : false,
				panelHeight : 'auto'
			});
			
			if('${sessionInfo.repertoryIds}'!=null&&'${sessionInfo.repertoryIds}'!='null'){
				$.ajax({
					url : '${pageContext.request.contextPath}/serial!noAuth_serial.html',
					data : {type : 'DB',rid : ${sessionInfo.repertoryIds}},
					type : "POST",
					success : function(d){
						$('#serial').val(d);
					}
				});
			} 
		
			$('#dg').datagrid({
				//idField : 'id',
				toolbar:[{
					text: '确定',
					iconCls:'icon-edit',
					handler:function(){
						$('#dg').datagrid('acceptChanges');
						sumNum();
					}
				},'-',{
					text: '删行',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#dg').datagrid('getSelected');
						if (row){
							var index = $('#dg').datagrid('getRowIndex', row);
							$('#dg').datagrid('deleteRow', index);
						}
						sumNum();
					}
				},'-',{
					text: '保存',
					iconCls:'icon-add',
					handler:function(){
						sumNum();
						$('#dg').datagrid('acceptChanges');
						
						var items = [];
						var nums = [];
						var rows = $('#dg').datagrid('getRows');
						var rs="";
						var sign=false;
						for ( var i = 0; i < rows.length; i++) {
							if(!isNaN(rows[i].id)&&rows[i].id!=null&&rows[i].id!='null'&&rows[i].id!='undefinded'
							&&!isNaN(rows[i].num)&&rows[i].num!=null&&rows[i].num!='null'&&rows[i].num!='undefinded'){			
								items.push(rows[i].id);
								nums.push(rows[i].num);
							}

							if(rows[i].num>rows[i].amount){
								rs+=rows[i].barcode+",";
								sign=true;
							}
							
						}
						if(sign){
						var isGO=true;
							  $.messager.confirm("警告","条形码为:"+rs+" 的商品库存数量大于调拨的数量,是否继续?", function (r) {  
							            isGo=r; 
							    });  
							if(isGo==false){
								return;
							}
						}

						
						$('#items').val(items.join(','));
						$('#nums').val(nums.join(','));
						var irid = $('#irid').val();
						var orid = $('#orid').val();
						if($.trim(irid).length>0&&$.trim(orid).length>0&&irid==orid){
							parent.sy.messagerAlert('提示', '调入仓库和调出仓库相同，为无效调拔！', 'error');
							return;
						}
						
						$('#transferForm').form('submit', {
							url : '${pageContext.request.contextPath}/kcgl/goodsTransferAction!add.html',
							
							success : function(d) {
								var json = $.parseJSON(d);
								parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
								});
								if (json.success) {
									parent.showTab("添加库存调拨单","库存调拨");
								}
							}
						});
					}
				}],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickRow:function(rowIndex){
					if (lastIndex != rowIndex){
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}else{
						$('#dg').datagrid('endEdit', lastIndex);
						$('#dg').datagrid('beginEdit', rowIndex);
					}
					lastIndex = rowIndex;
					sumNum();
				}
			});
			
			
			$("#barcode").live("keypress",function(e) {
				var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
				if (keyCode == 13) {
					_search();
					return false;
				}
				
			});
		});
	
		function _search() {
			var orid = $('input[name=orid]').val();
			if(orid.length<1){
				parent.sy.messagerAlert('提示', '请先选择调出仓库！', 'error');
				return;
			}
			$("#mytable").datagrid('loadData',{total:0,rows:[]});
			$('#searchRid').val(orid);
			
			$('#searchForm').form('submit', {
				url : '${pageContext.request.contextPath}/spgl/goodsAction!noAuth_search_return.html',
				success : function(d) {
					var json = $.parseJSON(d);
					if(json.success!=false){
					if(json.length>1){
						$('#mydialog').show(); 
						$('#mydialog').dialog({ 
						collapsible: true, 
						minimizable: false, 
						maximizable: false,
						closable:true,
						closed:false, 
						}); 
						for(var i=0;i<json.length;i++){
							 $("#mytable").datagrid('appendRow',{
							 
					            barcode:json[i].barcode,
					            type:json[i].type,
					            color:json[i].color,
					            size:json[i].size,
							    /*type:json.type,
							    gtname:json.gtname,
							    bname:json.bname,
							    name:json.name,
							    unit:json.unit,
							    color:json.color,
							    size:json.size,
							    //num:'1'
							    num:tempnum*/
						     });
						}
						$("#mytable").datagrid({
							toolbar:[{
								text:'确定',
								iconCls:'icon-save',
								handler:function(){
									var selections=$('#mytable').datagrid('getSelections');
									try{
								
										for(var i=0;i<selections.length;i++){
											var obj=selections[i];
											var index=$("#mytable").datagrid("getRowIndex",obj);
											//alert(index);
											//添加数据
											searchAddGoodsTransferItem(json,index);
										}

										//关闭
										$('#mydialog').dialog({
											closed:true,
										 });
										
										//searchSelectAddGoods(json,jsonid,num)
									
									}catch(e){
										alert(e);
									}
									
								}
							},{
								text:"取消选择",
								iconCls:'icon-undo',
								handler:function(){
									$("#mytable").datagrid("unselectAll");
								}
							}],
							onDblClickRow:function(rowIndex, rowData){
								searchAddGoodsTransferItem(json,rowIndex);
								/*
								$("#mytable").datagrid('loadData',{total:0,rows:[]});					
								$('#mydialog').dialog({
									closed:true,
								 });
								 $("#barcode").val("");
								$("#barcode").focus();
								*/
							}
						});
					}else{
						searchAddGoodsTransferItem(json,0);
						$("#barcode").val("");
						$("#barcode").focus();
					}
					}else{
						parent.sy.messagerShow({
									msg : json.msg,
									title : '提示'
						});
					}
					sumNum();
				}
			});
// 			cleanSearch();
		}
		
		function cleanSearch() {
			$('#searchForm input').val('');
		}
		//添加搜索的到商品
		function searchAddGoodsTransferItem(json,jsonid){
				var rows = $('#dg').datagrid('getRows');
			    var flag = 0;	
			    if(rows.length > 0){
			        for(var i = 0; i < rows.length; i++) {
			            var tempbarcode = json[jsonid].barcode;
			            if(tempbarcode == rows[i].barcode){	
			                tempnum = rows[i].num + 1;		   
			                $('#dg').datagrid('updateRow',{
	                                  index: i,
	                                  row: {
	                                     num: tempnum
	                                  }
	                                 });					                
			                flag = 1;
			                tempnum = 1;
			                return false;
			            }					            
				    }					        
			    }
			    if(flag == 0){
				$('#dg').datagrid('endEdit', lastIndex);
				$('#dg').datagrid('appendRow',{
					id:json[jsonid].id,
					barcode:json[jsonid].barcode,
					type:json[jsonid].type,
					gtname:json[jsonid].gtname,
					bname:json[jsonid].bname,
					name:json[jsonid].name,
					unit:json[jsonid].unit,
					color:json[jsonid].color,
					size:json[jsonid].size,
					amount:json[jsonid].amount,
					num: tempnum
				});
				lastIndex = $('#dg').datagrid('getRows').length-1;
				$('#dg').datagrid('selectRow', lastIndex);
			 }					    
			return false;
		}
		
		function sumNum(){
			var rows = $('#dg').datagrid('getRows');
			var sum = 0;
			for ( var i = 0; i < rows.length; i++) {
				sum += Number(rows[i].num);
			}
			$('#num').val(sum);
		}
	</script>
	</head>
	<body class="easyui-layout" data-options="fit:true">
	
		<div data-options="region:'north',border:false" style="height: 140px; overflow: hidden;">
			<form id="transferForm" style="height:110px;" mothod="post">
				<input id="items" name="items" type="hidden" />
				<input id="nums" name="nums" type="hidden" />
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 80px;">日&nbsp;&nbsp;&nbsp;&nbsp;期</th>
						<td style="width: 200px;"><input name="date" id="datebox" class="Wdate" readonly="readonly" onclick="WdatePicker()" style="width: 200px;" data-options="required:'true',missingMessage:'请输入调拔日期'"/></td>
						<th style="width: 80px;">单&nbsp;&nbsp;&nbsp;&nbsp;号</th>
						<td style="width: 200px;"><input name="serial" style="width: 200px;" readonly="readonly" id="serial"/></td>
						<th style="width: 80px;">操&nbsp;作&nbsp;员</th>
						<td style="width: 200px;"><input name="uid" id="uid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择操作员'"/></td>
					</tr>
					<tr>
						<th>调出仓库</th>
						<td><input name="orid" id="orid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择调出仓库'"/></td>
						<th>调入仓库 </th>
						<td><input name="irid" id="irid" style="width: 200px;" data-options="required:'true',missingMessage:'请选择调入仓库'"/></td>
						<th>调拨总数</th>
						<td><input name="num" id="num" style="width: 200px;" type="text" readonly="readonly"/></td>
					</tr>
					<tr>
						<th>备&nbsp;&nbsp;&nbsp;&nbsp;注</th>
						<td colspan="5"><textarea name="remark" style="height:30px;width: 95%;"></textarea></td>
					</tr>
				</table>
			</form>
			
			<form id="searchForm" style="height:10px;margin-top:0px;">
				<input type="hidden" name="rid" value="" id="searchRid"/>
				<table class="tableForm datagrid-toolbar" style="width: 100%; height: 100%;">
					<tr>
						<th style="width: 72px;">商品条码</th>
						<td style="width: 200px;"><input name="barcode" id="barcode"  style="width: 200px;" /></td>
 						<th style="width: 80px;">款&nbsp;&nbsp;&nbsp;&nbsp;号</th> 
 						<td style="width: 200px;"><input name="type" style="width: 200px;" /></td> 
						<td style="width: 280px;" align="right" colspan="4">
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="_search();">定位商品</a>
							<a href="javascript:void(0);" class="easyui-linkbutton" onclick="cleanSearch();">取消</a>
						</td>
					</tr>
				</table>
			</form>
		</div>
	
		<div data-options="region:'center',border:false" style="overflow: auto;">
			<table id="dg" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit',singleSelect: true">  
		        <thead>
		            <tr>
		            	<th data-options="field:'id',width:120,hidden:true">商品ID</th>
		            	<th data-options="field:'barcode',width:100">商品条码</th>
		            	<th data-options="field:'name',width:100">商品名称</th>
		            	<th data-options="field:'bname',width:80">品牌</th>
		            	<th data-options="field:'gtname',width:80">商品类别</th>  
		                <th data-options="field:'type',width:50,align:'center'">款号</th>  
                   		<th data-options="field:'color',width:50,align:'center'">颜色</th>
                   		<th data-options="field:'size',width:80,align:'center'">尺码</th>
                   		<th data-options="field:'unit',width:50,align:'center'">单位</th>
                   		<th data-options="field:'amount',width:80,align:'center'">库存数</th>
		                <th data-options="field:'num',width:120,align:'center',editor:{type:'numberbox'}">调拔总数</th>
		            </tr>
		        </thead>
		    </table>
		</div>
		<div id="mydialog" style="display:none;padding:0px;width:410px;height:auto;min-height:300px;max-height:300px;overflow-y:scrooll;" title="该条码对应了多件商品，请双击您要调拨的商品。(可多选)">		 		
		<table id="mytable" class="easyui-datagrid" style="height:auto"  data-options="iconCls: 'icon-edit'"> 
		        <thead>
		            <tr>
		            	<th data-options="field:'ck',width:80,checkbox:true">选择</th>
		            	<th data-options="field:'barcode',width:100">商品条码</th>  
		            	<th data-options="field:'type',width:100">款号</th>  
	                 	<!-- <th data-options="field:'name',width:100">商品名称</th> 
	                 	<th data-options="field:'bname',width:80">品牌</th> -->
                   		<th data-options="field:'color',width:80,align:'center'">颜色</th>
                   		<th data-options="field:'size',width:80,align:'center'">尺码</th>
		            </tr>
		        </thead>
		    </table>
		</div> 
	</body>
</html>